package com.yanqu.road.server.cmd.pageant;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.game.result.UserPageantDataResult;
import com.yanqu.road.pb.pageant.PageantProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.game2048.PageantModule;
import com.yanqu.road.server.gameplayer.module.player.PlayerModule;
import com.yanqu.road.server.manger.VipMgr;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.GamePageantMgr;
import com.yanqu.road.server.pb.PageantPb;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

@Cmd(code = Protocol.S_PAGEANT_MOVE, desc = "移动美女")
public class PageantMoveBeautyCmd extends AbstractCommand {

    private static Logger logger = LogManager.getLogger(PageantMoveBeautyCmd.class.getName());

    private int activityId;

    private int direction;

    private int type;

    private UserPageantDataResult result;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        PageantProto.PageantMoveBeautyReqMsg reqMsg = PageantProto.PageantMoveBeautyReqMsg.parseFrom(packet.getBytes());
        activityId = reqMsg.getActivityId();
        direction = reqMsg.getDirection();
        type = reqMsg.getType();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        ActivityInfo activityInfo = GamePageantMgr.getActivityInfo();
        if (activityInfo == null || !ActivityMgr.activityInTime(activityInfo)) {
            ret = GameErrorCode.E_ACTIVITY_NO_FOUND;
            return;
        }

        if (direction < 1 || direction > 4) {
            ret = GameErrorCode.E_PAGEANT_MOVEDIRECTION_EMPTY;
            return;
        }
        if (type != 1 && type != 2 && type != 3) {
            ret = GameErrorCode.E_PAGEANT_MODE_EMPTY;
            return;
        }

        if (type == 3 && GamePageantMgr.getFastUnlockVipLevel() == Integer.MAX_VALUE) {
            ret = GameErrorCode.E_PAGEANT_ACTIVITY_EMPTY;
            return;
        }

        if (type == 3 && VipMgr.getVipLv(player.getModule(PlayerModule.class).getUserInfo().getVipExp()) < GamePageantMgr.getFastUnlockVipLevel()) {
            ret = GameErrorCode.E_PAGEANT_FAST_NO_UNLOCK;
            return;
        }

        result = player.getModule(PageantModule.class).playGame(activityInfo.getActivityId(), direction, type);
    }


    @Override
    public void sendData(GamePlayer player) {
        PageantProto.PageantMoveBeautyRespMsg.Builder respMsg = PageantProto.PageantMoveBeautyRespMsg.newBuilder();
        if(0==ret){
            respMsg.setRet(result.getRet());
            if (0 == result.getRet() || GameErrorCode.E_PAGEANT_CAN_MOVE_OTHER == result.getRet()
                    || GameErrorCode.E_PAGEANT_CAN_NOT_MOVE == result.getRet() || GameErrorCode.E_PAGEANT_STRENGTH_NOT_ENOUGH == result.getRet()) {
                if(null!=result.getUserPageantData()) {
                    respMsg.setEntity(PageantPb.parsePageantBaseRespMsg(result.getUserPageantData()));
                }
                if (null != result.getReward() && !result.getReward().equals("")) {
                    respMsg.setReward(result.getReward());//返回奖励
                }
            }
        }else {
            respMsg.setRet(ret);
        }
        player.sendPacket(Protocol.U_PAGEANT_MOVE, respMsg);
    }
}
